<template>
    <div class="login_container">
        <!--    登录表单-->
        <div class="login_box">
            <el-form class="login_form" :model="login_form" :rules="login_form_rules">
                <!--      页头字样-->
                <el-form-item>
                    <span class="label">Login</span>
                </el-form-item>
                <!--        用户名-->
                <el-form-item prop="username">
                    <el-input prefix-icon="el-icon-user" placeholder="用户名" v-model="login_form.username"></el-input>
                </el-form-item>
                <!--        密码-->
                <el-form-item prop="password">
                    <el-input prefix-icon="el-icon-lock" placeholder="密码" v-model="login_form.password" show-password></el-input>
                </el-form-item>
                <!--        按钮-->
                <el-form-item class="buttons">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="primary" @click="register">注册账号</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data () {
            return {
                login_form: {
                    username: '',
                    password: ''
                },
                login_form_rules: {
                    username: [
                        { required: true, message: '用户名不能为空', trigger: 'blur' },
                        { min: 3, max: 15, message: '用户名长3-15位', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            register () {
                this.$router.push('/register')
            },
            login () {
                this.$http.post('login',this.login_form).then(res => {
                    console.log(res.data)
                    if(res.data.code === 200){
                        this.$message({
                            message: '登录成功',
                            type: 'success'
                        })

                        //    将用户名和token放入sessionStorage
                        sessionStorage.setItem('userName', res.data.username)
                        sessionStorage.setItem('userToken', res.data.token)
                        //    将用户名和token放入vuex
                        this.$store.dispatch('setUser', res.data.username)
                        this.$store.dispatch('setToken', res.data.token)

                        console.log(this.$store.state.token)
                        //跳转
                        this.$router.push('/home')
                    }
                    else if (res.data.code === 400){
                        this.$message.error('输入格式错误')
                    }
                    else if ((res.data.code === 403) && (res.data.type === 'password_error')) {
                        this.$message.error('密码错误')
                    }
                    else if ((res.data.code === 403) && (res.data.type === 'user_non_exist')) {
                        this.$message.error('用户名未注册')
                    }
                    else{
                        this.$message.error('未知错误')
                    }
                })
            }

        }
    }
</script>

<style lang="less" scoped>
    .login_container{
        background-color: #fff;
        height: 100%;
    }

    .login_box{
        width: 450px;
        height: 270px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        box-shadow: 0 0 5px 3px rgb(221,221,221);
    }
    .label{
        text-align: center;
        display: block;
        font-size: 22px;
        font-weight: bold;
        color: rgb(105, 105, 105);
    }
    .buttons{
        display: flex;
        justify-content: flex-end;
    }

    .login_form{
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 40px;
        box-sizing: border-box;
    }
</style>
